home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / Trial / Paint Shop Pro XI / Data1.cab / pspscriptpyblocks.py < prev    next >
Encoding:
Python Source  |  2006-08-04  |  11.3 KB  |  290 lines

  1. ######################
  2. #
  3. #   PyBlock class
  4. #
  5. ######################
  6.  
  7. import PSPTools
  8. from PSPTools import *
  9.  
  10. class PyBlock:
  11.     def __init__(self, cmd, block):
  12.         self.commandType = cmd            # type of block found
  13.         self.block = block                # lines of text for block
  14.         self.modified = 0                 # has block been modified.
  15.         self.exeModeAction = 'nochange'   # code indicating change of execution mode on write
  16.         self.newCmdBody = 'nochange'      # if the user edited a command thru its dialog, then this
  17.                                           # string will have the updated parameters
  18.         self.editable = 0                 # for commands only: is this command editable
  19.         self.deleted = 0
  20.         self.commentbutton = 666
  21.         return
  22.  
  23.     # this function should toggle
  24.     def commentHandler(self):
  25.         # if we are commenting, then copy the block to newCmdBody with comment markers
  26.         # otherwise just copy the block to newCmdBody with no modifications
  27.         commented = not self.commentAction.get()
  28.         copied = false
  29.  
  30.         self.modified = 1            
  31.  
  32.         print 'COMMENTED'
  33.         print commented
  34.         print 'end COMMENTED'
  35.  
  36.         # update the text
  37.         if commented :
  38.             self.commentbutton.config(text='Disabled')
  39.         else:
  40.             self.commentbutton.config(text='Enabled ')
  41.         
  42.         if  self.newCmdBody == 'nochange':
  43.             self.newCmdBody = self.block
  44.             copied = true
  45.             
  46.     # comment the block
  47.         if (self.commandType == 'PSPCmd' and commented) or (self.commandType == 'CmtedPSPCmd' and commented and not copied):
  48.             if self.newCmdBody == 'nochange':
  49.                 self.newCmdBody = self.block
  50.  
  51.             # insert a comment marker at the beginning
  52.             self.newCmdBody = '#' + self.newCmdBody
  53.  
  54.             insertlist = []
  55.             pos = 0
  56.  
  57.             # if we find a newline char, put its index location into a list
  58.             for x in self.newCmdBody:
  59.                 if x == chr(10):
  60.                     insertlist += [pos]
  61.                 pos += 1
  62.  
  63.             blockLen = len(self.newCmdBody)
  64.             stringCtr = blockLen
  65.             posCtr = len(insertlist)-1
  66.  
  67.             # cycle backwards thru the block, inserting a '#' at all marked locations
  68.             while posCtr>=0:
  69.                 self.newCmdBody = self.newCmdBody[:insertlist[posCtr]+1] + '#' + self.newCmdBody[insertlist[posCtr]+1:]
  70.                 posCtr -= 1
  71.  
  72.             self.modified = 1
  73.  
  74.         # remove comments from the block
  75.         elif (self.commandType == 'CmtedPSPCmd' and not commented) or (self.commandType == 'PSPCmd' and not commented and not copied):
  76.             if self.newCmdBody == 'nochange':
  77.                 self.newCmdBody = self.block
  78.             # deletelist is a list of char positions that we will delete
  79.             deletelist = []
  80.  
  81.             blockSize = len(self.newCmdBody)
  82.  
  83.             pos = 0
  84.             # mark the position of all hash marks in the command text for deletion
  85.             while pos < blockSize-1:
  86.                 while self.newCmdBody[pos] == '#' or self.newCmdBody[pos] == ' ' or self.newCmdBody[pos] == chr(9):
  87.                     if self.newCmdBody[pos] == '#':
  88.                         deletelist += [pos]
  89.                     pos += 1
  90.  
  91.                 while self.newCmdBody[pos] != chr(10) and pos < blockSize-1:
  92.                     pos += 1
  93.                 if self.newCmdBody[pos] == chr(10):
  94.                     pos += 1
  95.  
  96.             pos = len(deletelist)-1
  97.             # delete the hash marks from the command text
  98.             while pos >= 0:
  99.                 delIndex = deletelist[pos]
  100.                 cmdText = self.newCmdBody
  101.                 self.newCmdBody = cmdText[:delIndex] + cmdText[delIndex+1:]
  102.                 pos -= 1
  103.  
  104.             self.modified = 1
  105.  
  106.     def exeModeHandler(self, newmode):
  107.         if self.newCmdBody == 'nochange':
  108.             self.newCmdBody = self.block
  109.         self.newCmdBody = PSPTools.changeExecutionMode(self.newCmdBody, newmode)
  110.         self.modified = 1
  111.         print newmode
  112.  
  113.     def selectionHandler():
  114.         print self.selected.get()
  115.  
  116.     def dump(self):
  117.         return (self.block, self.commandType)
  118.  
  119.     def write(self, fp):
  120.         fp.write(self.block)
  121.         return
  122.  
  123.     def edit(self):
  124.         return
  125.  
  126.  
  127. #############################################
  128. class PyScript(PyBlock):
  129.     def __init__(self, cmd, block, parameters):
  130.         # initialize the base class first
  131.         PyBlock.__init__ (self, cmd, block)
  132.         
  133.         # init self.  
  134.  
  135.         if parameters.has_key("Author"):
  136.             self.auth = parameters["Author"]
  137.         else:
  138.             self.auth = ""
  139.         if parameters.has_keyself("Copyright"):
  140.             self.copy = parameters["Copyright"]
  141.         else:
  142.             self.copy = ""
  143.         if parameters.has_key("Description"):
  144.             self.desc = parameters("Description")
  145.         else:
  146.             self.desc = ""
  147.         if parameters.has_key("Host"):
  148.             self.host = parameters("Host")
  149.         else:
  150.             self.host = ""
  151.         if parameters.has_key("Host Version"):
  152.             self.hostver = parameters["Host Version"]
  153.         else:
  154.             self.hostver = ""
  155.  
  156.     def write(self, fp):
  157.     
  158.         # if a change has been made, write anew, else write original....
  159.         if self.modified:
  160.             fp.write("def ScriptProperties():\n    return {")
  161.             fp.write("\n        'Author': ")
  162.             fp.write(self.auth)
  163.             fp.write(",\n        'Copyright': ")
  164.             fp.write(self.copy)
  165.             fp.write(",\n        'Description': ")
  166.             fp.write(self.desc)
  167.             fp.write(",\n        'Host': ")
  168.             fp.write(self.host)
  169.             fp.write(",\n        'Host Version': ")
  170.             fp.write(self.hostver, "\n        }\n")
  171.         else:
  172.             fp.write(self.block)            
  173.  
  174. #############################################
  175. class PyPSPCmd(PyBlock):
  176.     def __init__(self, cmd, block, cmdname, parameters):
  177.         # initialize the base class first
  178.         PyBlock.__init__ (self, cmd, block)
  179.  
  180.         self.cmdname = cmdname
  181.         self.edited = 0
  182.         self.parms = parameters         # csl.  may not need....
  183.         if cmd == "PSPCmd":
  184.             self.commented = 0              # command is hot and not commented out
  185.         else:
  186.             self.commented = 1
  187.         self.initialCommented = self.commented
  188.             
  189.         # call app to determine if the command is editable...  csl...
  190.         self.isEditable = 1
  191.         
  192.         # set the execution mode
  193.         if block.find('App.Constants.ExecutionMode.Default'):
  194.             self.exmode = "default"
  195.         elif block.find('App.Constants.ExecutionMode.Silent'):
  196.             self.exmode = "silent"
  197.         elif block.find('App.Constants.ExecutionMode.Verbose'):   # csl swagged label..
  198.             self.exmode = "verbose"
  199.         else:
  200.             self.exmode = "defaultNotCoded"
  201.         self.initialExMode = self.exmode
  202.  
  203.     def write(self, fp):
  204.     
  205.         # if a change has been made, write anew, else write original....
  206.         if self.modified:
  207.             
  208.             # if execution mode has been changed then modify it...  ok, whack it in...
  209.             if self.initialExMode <> self.exmode:
  210.                 if self.initialExMode == "silent":
  211.                     # change from silent to something new
  212.                     if self.exmode == "verbose":
  213.                         block.replace("'App.Constants.ExecutionMode.Silent'",
  214.                                       "'App.Constants.ExecutionMode.Verbose'", 1)
  215.                     else:
  216.                         block.replace("'App.Constants.ExecutionMode.Silent'",
  217.                                       "'App.Constants.ExecutionMode.Defau1t'", 1)
  218.                     
  219.                 elif self.initialExMode == "verbose":
  220.                     # change from verbose to something new
  221.                     if self.exmode == "silent":
  222.                         block.replace("'App.Constants.ExecutionMode.Verbose'",
  223.                                       "'App.Constants.ExecutionMode.Silent'", 1)
  224.                     else:
  225.                         block.replace("'App.Constants.ExecutionMode.Verbose'",
  226.                                       "'App.Constants.ExecutionMode.Defau1t'", 1)
  227.                     
  228.                 elif self.initialExMode == "default":
  229.                     # change from verbose to something new
  230.                     if self.exmode == "silent":
  231.                         block.replace("'App.Constants.ExecutionMode.Default'",
  232.                                       "'App.Constants.ExecutionMode.Silent'", 1)
  233.                     else:
  234.                         block.replace("'App.Constants.ExecutionMode.Default'",
  235.                                       "'App.Constants.ExecutionMode.Verbose'", 1)
  236.   
  237.                 else:
  238.                     # initial state was default not coded.  insert into general settings.
  239.  
  240.                     if self.exmode == "verbose":
  241.                         exModeToInsert = "            'App.Constants.ExecutionMode.Verbose'"
  242.                     else:
  243.                         exModeToInsert = "            'App.Constants.ExecutionMode.Silent'"
  244.                          
  245.                     if block.find("'GeneralSettings': {"):
  246.                         block.replace("'GeneralSettings': {",
  247.                                       "'GeneralSettings': {\n" + exModeToInsert, 1)
  248.                     else:
  249.                         # no general settings found.  insert after open brace
  250.                         if block.find("{"):
  251.                             block.replace("{",
  252.                                       "'{\n            'GeneralSettings': {\n" + exModeToInsert + "},", 1)
  253.  
  254.  
  255.             # if commented state has been changed from initial, change it...
  256.             if self.initialCommented <> self.commented:
  257.                 if self.initialCommented:
  258.                     # was initially commented and now is not.  put a space in char pos 1
  259.                     # for all lines in the command
  260.                     i = 0
  261.                     newline = 1
  262.                     while i < len(block):
  263.                         if newline and block[i] == '#':
  264.                             block [i] = ' '
  265.                         else:
  266.                             newline = 0
  267.                             if block[i] == '\n':
  268.                                 newline = 1
  269.                         i = i + 1
  270.                 else:
  271.                     # was initially not commented and now it should be.  put a # in char pos
  272.                     # 1 for all lines in the command
  273.                     i = 0
  274.                     newline = 1
  275.                     while i < len(block):
  276.                         if newline and block[i] == ' ':
  277.                             block [i] = '#'
  278.                         else:
  279.                             newline = 0
  280.                             if block[i] == '\n':
  281.                                 newline = 1
  282.                         i = i + 1
  283.                         
  284. #            if self.edited:
  285.                 # call the application to reform the parms.  or was this done at edit time
  286.                 # csl ??????
  287.             
  288.         else:
  289.             fp.write(self.block)            
  290.